/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun;

import com.xerox.amazonws.ec2.*;
import com.norbl.util.*;
import java.io.*;
import java.util.*;
import com.norbl.util.s3.*;

/** A {@link RunnerNode} that runs on sonmething other than an Ec2
 *  instance.  This class gets its user data from a file (specified
 *  as argv[0]) and writes throwables to the stdout.  Otherwise it
 *  is identical to {@link RunnerNodeEc2}.
 *
 * @author Barnet Wagman
 */
public class RunnerNodeNonEc2 extends RunnerNode {

    private HashMap<String,Integer> indexHt;
    static File paramFile;

    public static void main(String[] argv) {
        try {
            if ( argv.length != 1 ) {
                System.out.println(usage());
                return;
            }
            paramFile = new File(argv[0]);
            if ( !paramFile.exists() || paramFile.isDirectory() ) {
                System.out.println("There is no param file named " +
                                   paramFile.getPath());
                return;
            }

            System.out.println("Getting node params from " + paramFile.getPath());
            RunnerNodeNonEc2 lne = new RunnerNodeNonEc2();
            lne.run();
        }
        catch(Throwable x) {
            System.out.println(StringUtil.throwableToString(x));
        }
    }

    static String usage() {
        return("Usage: java ... com.norbl.runjrun.RunnerNodeLocal " +
               "<full path to param file>");
    }

    public RunnerNodeNonEc2() {
        super();        
        indexHt = new HashMap<String,Integer>();        
    }

    String getInstanceID() {
        return(host + "-" + Constants.NOT_EC2);
    }

    protected String getUserData() {
        System.err.println("The param file is " + paramFile.getPath()  +
                " size=" + paramFile.length());
        return( FileUtil.readFile(paramFile) );
    }

    protected void handleThrowable(Throwable xxx) {       
        System.out.println(StringUtil.throwableToString(xxx));
    }

    String buildThrowableS3Key(Throwable x) {

        String s = params.exceptionS3KeyPrefix + "/";
        if ( x != null ) s += x.getClass().getSimpleName();
        else s += "null-throwable";

        if ( indexHt.size() > 1024 ) indexHt = new HashMap<String, Integer>();

        s += "-" + Time.toDateTimeMilliKeyString(System.currentTimeMillis());
        s += "-" + StringUtil.getNextIndex(indexHt,s);
        s += "." + S3Ops.GZSER;

        return(s);
    }
}
